<!-- 导出新生名单 -->
<template>
    <div class="edu-page-warp">
        <condition-card
            @reset="reset"
            @search="onSubmit"
            label-width="110px"
            label-position="right"
        >
            <el-row>
                <el-col :span="8">
                    <el-form-item label="年级">
                        <grade-select
                            v-model="queryForm.grade"
                            :show-all="false"
                        ></grade-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="学院">
                        <DictSelect
                            labelAll="请选择"
                            v-model="queryForm.faculty"
                            :data="options.faculty"
                            @change="changeFaculty"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="专业">
                        <DictSelect
                            labelAll="请选择"
                            v-model="queryForm.profession"
                            :data="options.profession"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="是否留学生">
                        <DictSelect
                            labelAll="请选择"
                            v-model="queryForm.isOverseas"
                            :data="options.isOverseas"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
            </el-row>
        </condition-card>
        <el-card shadow="never">
            <h2 class="edu-page-title">学生列表</h2>
            <el-row type="flex" class="mb-8px">
                <el-col>
                    <KeywordInput v-model="queryForm.stuName" @enter="filter" placeholder="学号或姓名搜索"></KeywordInput>
                </el-col>
                <el-col class="text-right">
                    <el-button type="primary" @click="exportFm" size="mini" >导出封面</el-button>
                    <el-button type="primary" @click="exportStuDir" size="mini" >导出专业目录</el-button>
                    <el-button type="primary" @click="exportStuList" size="mini" >导出新生名单</el-button>
                </el-col>
            </el-row>
            <edu-table
                :order="false"
                :pageNum="queryForm.pageNum_"
                :data="studentStatusList.data"
                :total="studentStatusList.total"
                @page-change="pageChange"
                @selection-change="handleSelectionChange"
            >
                <el-table-column label="学号" prop="stuId" fixed></el-table-column>
                <el-table-column label="姓名" fixed>
                    <template slot-scope="scope">{{scope.row.stuName}}</template>
                </el-table-column>
                <el-table-column label="性别" prop="stuSex"></el-table-column>
                <el-table-column label="出生日期" prop="stuBirthday" show-overflow-tooltip></el-table-column>
                <el-table-column label="年级" prop="stuGrade"></el-table-column>
                <el-table-column label="学院" show-overflow-tooltip>
                    <template slot-scope="scope">{{scope.row.stuFaculty}}</template>
                </el-table-column>
                <el-table-column label="专业" show-overflow-tooltip>
                    <template slot-scope="scope">{{scope.row.stuProfession}}</template>
                </el-table-column>
                <el-table-column label="是否留学生" prop="stuIsOverseas">
                    <template slot-scope="scope">{{scope.row.stuIsOverseas === '0' ? '否' : '是'}}</template>
                </el-table-column>
                <el-table-column label="民族" prop="stuNation"></el-table-column>
                <el-table-column label="省市" prop="stuCity"></el-table-column>
                <el-table-column label="备注" prop="other"></el-table-column>
            </edu-table>
        </el-card>
        <edu-export-dialog class="test" :title="$t('message.prompt')" ref="downloadRef" ></edu-export-dialog>
    </div>
</template>
<script>
import { findViewColumnByTable, exportFm, exportStuList, exportStuDir, queryFileStatus } from 'common/src/api/studentinfo'
import UTILS from 'common/src/utils/utils'
import { queryDic } from 'common/src/api/dictionary'
export default {
    name: 'freshmanList',
    data() {
        return {
            queryForm: {
                grade: '', //年级
                faculty: '', //学院
                profession: '', //专业
                isOverseas: "", //是否留学生
                stuName: '', // 学生名字
                stuId: '',// 学生学号
                pageNum_: 1,
                pageSize_: 20
            },
            options: {
                faculty: [],
                profession: [],
                trainingLevel: [],
                trainingCategory: [],
                degreeType: [],
                enrolSeason: [],
                formLearning: [],
                registerStatus: [
                    {
                        label: '未报到',
                        value: '0'
                    },
                    {
                        label: '已报到',
                        value: '1'
                    }
                ],
                scanResult: [
                    {
                        label: '未扫描',
                        value: 'N'
                    },
                    {
                        label: '已扫描',
                        value: 'Y'
                    }
                ],
                faceResult: [
                    {
                        label: '未通过',
                        value: 'N'
                    },
                    {
                        label: '已通过',
                        value: 'Y'
                    }
                ],
                isOverseas: []
            },
            studentStatusList: {
                data: [],
                total: 0
            },
            selectedItems: [],
            currentDptId: ''
        }
    },
    created() {
        if (this.mockUser) {
            this.currentDptId = this.mockUser.currentManageDptId
        } else if (this.currentUser) {
            this.currentDptId = this.currentUser.currentManageDptId
        }
        this.init()
        this.onSubmit()
    },
    methods: {
        // 初始化获取筛选条件字典值
        init() {
            //year
            let year = new Date().getFullYear()
            this.queryForm.grade = year
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: [
                    'X_YX',
                    'X_PYCC',
                    'X_PYLB',
                    'X_XWLX',
                    'X_XXXS',
                    'isAborad',
                    'X_RXJJ',
                    'L_LXZT'
                ]
            }
            queryDic(data)
                .then(res => {
                    if (res.code == 200) {
                        UTILS.fillSelect(
                            {
                                faculty: 'X_YX',
                                trainingLevel: 'X_PYCC',
                                trainingCategory: 'X_PYLB',
                                degreeType: 'X_XWLX',
                                enrolSeason: 'X_RXJJ',
                                formLearning: 'X_XXXS',
                                isOverseas: "isAborad"
                            },
                            res.data,
                            this.options
                        )
                    }
                })
                .catch(err => {
                    console.log(err)
                })
        },
        //学院改变获取专业
        changeFaculty(val) {
            this.options.profession = []
            this.queryForm.profession = ''
            let facultys, degreeType
            facultys = this.queryForm.faculty ? [this.queryForm.faculty] : []
            degreeType = this.queryForm.degreeType ? [this.queryForm.degreeType] : []
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: ['G_ZY'],
                filter: {
                    prefix: facultys,
                    specilaCode: 'XY2ZY',
                    facultys: facultys,
                    grade: this.queryForm.grade,
                    degreeType: degreeType
                }
            }
            queryDic(data)
                .then(res => {
                    if (res.code == 200)
                        UTILS.fillSelect(
                            {
                                profession: 'G_ZY'
                            },
                            res.data,
                            this.options
                        )
                })
                .catch(error => {
                    console.log(error)
                })
        },
        // 过滤条件重置
        reset() {
            let queryForm = this.queryForm
            for (var key in queryForm) {
                if (key.includes('grade')) {
                    queryForm[key] = new Date().getFullYear()
                } else if (key.includes('pageNum_')) {
                    queryForm[key] = 1
                } else if (key.includes('pageSize_')) {
                    queryForm[key] = 20
                } else if (key.includes('faculty')) {
                    queryForm[key] = ''
                } else if (key.includes('profession')) {
                    queryForm[key] = ''
                } else if (key.includes('isOverseas')) {
                    queryForm[key] = ''
                } else if (key.includes('stuName')) {
                    queryForm[key] = ''
                }
            }
            this.options.profession = []
            this.findViewColumnByTable()
        },
        // 查询学生列表数据
        findViewColumnByTable () {
            findViewColumnByTable({
                stuGrade: this.queryForm.grade,
                stuFaculty: this.queryForm.faculty,
                stuIsOverseas: this.queryForm.isOverseas,
                stuProfession: this.queryForm.profession,
                stuName: this.queryForm.stuName,
                stuId: this.queryForm.stuId,
                pageNum_ : this.queryForm.pageNum_,
                pageSize_: this.queryForm.pageSize_
            }).then(res => {
                if (res.code == 200) {
                    this.studentStatusList = {
                        data: res.data.list,
                        total: res.data.total_
                    }
                }
            })
        },
        onSubmit() {
            this.queryForm.pageNum_ = 1
            this.findViewColumnByTable()
        },
        pageChange(val) {
            this.queryForm.pageNum_ = val.pageNum
            this.queryForm.pageSize_ = val.pageSize
            this.findViewColumnByTable()
        },
        handleSelectionChange(val) {
            this.selectedItems = val
        },
        filter() {
            this.onSubmit()
        },
        //导出封面
        exportFm() {
            exportFm(this.queryForm.grade).then()
        },
        //导出专业目录
        exportStuDir() {
            exportStuDir(this.queryForm.grade).then()
        },
        // 导出新生名单
        exportStuList() {
            this.$refs.downloadRef.open()
            exportStuList(this.queryForm.grade).then((res) => {
                if (res.data) {
                    this.queryFileStatus(res.data)
                }
            })
        },
        queryFileStatus(key) {
            setTimeout(() => {
                queryFileStatus(key).then((resS) => {
                    let exportData = JSON.parse(resS.msg)
                    if (exportData.status === '1') {
                        this.$message({
                            message: this.$t('base.exportSuccess'),
                            type: 'success',
                        })
                        window.open(exportData.file)
                        this.$refs.downloadRef.close()
                    } else if(exportData.status === '0'){
                        this.queryFileStatus(key)
                    } else if(exportData.status === '2'){
                        this.$message({
                            message: this.$t('base.exportError'),
                            type: 'error',
                        })
                        this.$refs.downloadRef.close()
                    }
                })
            }, 3000)
        }
    },
    computed: {
        currentUser() {
            if (this.$store.state.session.user) {
                return this.$store.state.session.user
            }
            return JSON.parse(localStorage.getItem('user'))
        },
        mockUser() {
            if (this.$store.state.session.mockUser) {
                return this.$store.state.session.mockUser
            }
            let sessiondata = JSON.parse(localStorage.getItem('sessiondata'))
            if (sessiondata && sessiondata.mockUser) {
                return sessiondata.mockUser
            }
        }
    },
    filters: {
        getIsArrears(val) {
            let status = ''
            switch (val) {
                case 0:
                    status = '否'
                    break
                case 1:
                    status = '是'
                    break
                default:
                    status
            }
            return status
        }
    }
}
</script>
<style scoped>
.demo-table-expand .el-form-item__label {
    color: red !important;
}
.demo-table-expand .el-form-item {
    margin: 0;
}
.demo-table-expand .el-form-item .img1 {
    width: 90px;
    height: 120px;
    border: 1px solid #d2d4d6;
    margin-top: 12px;
}
.demo-table-expand .el-form-item .img2 {
    width: 160px;
    height: 120px;
    border: 1px solid #d2d4d6;
    margin-top: 12px;
}
</style>
